﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using TrainingsApp.Model;
using TrainingsApp.Helper;
using WisDragon.Framework.Data.DataAccess;

namespace TrainingsApp.DAL
{
    public class Sys_Permission_DAL
    {
        #region 功能管理
        public static T_FUNCTION_Entity T_FUNCTION_View(System.Int64 ID)
        {
            T_FUNCTION_Entity c = default(T_FUNCTION_Entity);
            try
            {
                DataCommand cmd = DataCommandManager.GetDataCommand("T_FUNCTION_View");
                cmd.SetParameterValue("@ID", ID);
                c = cmd.ExecuteEntity<T_FUNCTION_Entity>();
            }
            catch (Exception ex)
            {
            }
            return c;
        }

        public static T_FUNCTION_Entity T_FUNCTION_Add(T_FUNCTION_Entity entity)
        {
            try
            {
                DataCommand cmd = DataCommandManager.GetDataCommand("T_FUNCTION_Add");
                cmd.SetParameterValue("@FUNCTIONNAME", entity.FUNCTIONNAME);
                cmd.SetParameterValue("@DESCRIPTION", entity.DESCRIPTION);
                cmd.SetParameterValue("@LINKURL", entity.LINKURL);
                cmd.SetParameterValue("@PARENTID", entity.PARENTID);
                cmd.SetParameterValue("@CREATEDATE", entity.CREATEDATE);
                cmd.SetParameterValue("@SORT", entity.SORT);
                cmd.ExecuteNonQuery();
                entity.ID = Convert.ToInt32(cmd.GetParameterValue("@PID"));
            }
            catch (Exception ex)
            {
            }

            return entity;
        }

        public static void T_FUNCTION_Update(T_FUNCTION_Entity entity)
        {
            try
            {
                DataCommand cmd = DataCommandManager.GetDataCommand("T_FUNCTION_Update");
                cmd.SetParameterValue("@ID", entity.ID);
                cmd.SetParameterValue("@FUNCTIONNAME", entity.FUNCTIONNAME);
                cmd.SetParameterValue("@DESCRIPTION", entity.DESCRIPTION);
                cmd.SetParameterValue("@LINKURL", entity.LINKURL);
                cmd.SetParameterValue("@PARENTID", entity.PARENTID);
                cmd.SetParameterValue("@SORT", entity.SORT);
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
            }
        }

        public static void T_FUNCTION_Delete(System.Decimal ID)
        {
            try
            {
                DataCommand cmd = DataCommandManager.GetDataCommand("T_FUNCTION_Delete");
                cmd.SetParameterValue("@ID", ID);
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
            }
        }

        /// <summary>
        /// 功能记录数
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static int T_FUNCTION_RecordCount(Dictionary<string, object> parameters)
        {
            int RecordCount = 0;
            try
            {
                DataCommand cmd = DataCommandManager.GetDataCommand("T_FUNCTION_RecordCount");
                Utility.BuilderCommandParameter(parameters, cmd);
                RecordCount = Convert.ToInt32(cmd.ExecuteScalar());
            }
            catch (Exception ex)
            {
            }
            return RecordCount;
        }

        /// <summary>
        /// 功能分页列表
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public static List<T_FUNCTION_Entity> T_FUNCTION_ListByPaging(Dictionary<string, object> parameters, int pageSize, int pageIndex, out int recordCount)
        {
            List<T_FUNCTION_Entity> list = new List<T_FUNCTION_Entity>();
            recordCount = 0;
            try
            {
                DataCommand cmd = DataCommandManager.GetDataCommand("T_FUNCTION_ListByPaging");
                Utility.BuilderCommandParameter(parameters, cmd);
                cmd.SetParameterValue("@pageSize", pageSize);
                cmd.SetParameterValue("@pageIndex", pageIndex);
                list = cmd.ExecuteEntityList<T_FUNCTION_Entity>();
                recordCount = Convert.ToInt32(cmd.GetParameterValue("@recordCount"));
            }
            catch (Exception ex)
            {
            }
            return list;
        }

        /// <summary>
        /// 父功能列表
        /// </summary>
        /// <returns></returns>
        public static List<T_FUNCTION_Entity> T_FUNCTION_GetParentFunction()
        {
            List<T_FUNCTION_Entity> list = new List<T_FUNCTION_Entity>();
            try
            {
                DataCommand cmd = DataCommandManager.GetDataCommand("T_FUNCTION_GetParentFunction");
                list = cmd.ExecuteEntityList<T_FUNCTION_Entity>();
            }
            catch (Exception ex)
            {
            }
            return list;
        }

        /// <summary>
        /// 功能全列表
        /// </summary>
        /// <returns></returns>
        public static List<T_FUNCTION_Entity> T_FUNCTION_GetAllFunction()
        {
            List<T_FUNCTION_Entity> list = new List<T_FUNCTION_Entity>();
            try
            {
                DataCommand cmd = DataCommandManager.GetDataCommand("T_FUNCTION_GetAllFunction");
                list = cmd.ExecuteEntityList<T_FUNCTION_Entity>();
            }
            catch (Exception ex)
            {
            }
            return list;
        }

        /// <summary>
        /// 根据用户取功能列表
        /// </summary>
        /// <returns></returns>
        public static List<T_FUNCTION_Entity> T_Function_ListByUserId(System.Int64 UserId)
        {
            List<T_FUNCTION_Entity> list = new List<T_FUNCTION_Entity>();
            try
            {
                DataCommand cmd = DataCommandManager.GetDataCommand("T_Function_ListByUserId");
                cmd.SetParameterValue("@USERID", UserId);
                list = cmd.ExecuteEntityList<T_FUNCTION_Entity>();
            }
            catch (Exception ex)
            {
            }
            return list;
        }
        #endregion

        #region 角色管理

        public static T_ROLE_Entity T_ROLE_View(System.Decimal ID)
        {
            T_ROLE_Entity c = default(T_ROLE_Entity);
            try
            {
                DataCommand cmd = DataCommandManager.GetDataCommand("T_ROLE_View");
                cmd.SetParameterValue("@ID", ID);
                c = cmd.ExecuteEntity<T_ROLE_Entity>();
            }
            catch (Exception ex)
            {
            }
            return c;
        }

        public static T_ROLE_Entity T_ROLE_Add(T_ROLE_Entity entity)
        {
            try
            {
                DataCommand cmd = DataCommandManager.GetDataCommand("T_ROLE_Add");
                cmd.SetParameterValue("@ROLENAME", entity.ROLENAME);
                cmd.SetParameterValue("@DESCRIPTION", entity.DESCRIPTION);
                cmd.SetParameterValue("@CREATEDATE", entity.CREATEDATE);
                cmd.ExecuteNonQuery();
                entity.ID = Convert.ToInt32(cmd.GetParameterValue("@PID"));
            }
            catch (Exception ex)
            {
            }
            return entity;
        }

        public static void T_ROLE_Update(T_ROLE_Entity entity)
        {
            try
            {
                DataCommand cmd = DataCommandManager.GetDataCommand("T_ROLE_Update");
                cmd.SetParameterValue("@ID", entity.ID);
                cmd.SetParameterValue("@ROLENAME", entity.ROLENAME);
                cmd.SetParameterValue("@DESCRIPTION", entity.DESCRIPTION);
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
            }
        }

        public static void T_ROLE_Delete(System.Decimal ID)
        {
            try
            {
                DataCommand cmd = DataCommandManager.GetDataCommand("T_ROLE_Delete");
                cmd.SetParameterValue("@ID", ID);
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
            }
        }

        /// <summary>
        /// 角色记录数
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static int T_Role_RecordCount(Dictionary<string, object> parameters)
        {
            int RecordCount = 0;
            try
            {
                DataCommand cmd = DataCommandManager.GetDataCommand("T_Role_RecordCount");
                Utility.BuilderCommandParameter(parameters, cmd);
                RecordCount = Convert.ToInt32(cmd.ExecuteScalar());
            }
            catch (Exception ex)
            {
            }
            return RecordCount;
        }

        /// <summary>
        /// 角色分页列表
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public static List<T_ROLE_Entity> T_Role_ListByPaging(Dictionary<string, object> parameters, int pageSize, int pageIndex, out int recordCount)
        {
            List<T_ROLE_Entity> list = new List<T_ROLE_Entity>();
            recordCount = 0;
            try
            {
                DataCommand cmd = DataCommandManager.GetDataCommand("T_Role_ListByPaging");
                Utility.BuilderCommandParameter(parameters, cmd);
                cmd.SetParameterValue("@pageSize", pageSize);
                cmd.SetParameterValue("@pageIndex", pageIndex);
                list = cmd.ExecuteEntityList<T_ROLE_Entity>();
                recordCount = Convert.ToInt32(cmd.GetParameterValue("@recordCount"));
            }
            catch (Exception ex)
            {
            }
            return list;
        }

        /// <summary>
        /// 角色全列表
        /// </summary>
        /// <returns></returns>
        public static List<T_ROLE_Entity> T_Role_GetAllRole()
        {
            List<T_ROLE_Entity> list = new List<T_ROLE_Entity>();
            try
            {
                DataCommand cmd = DataCommandManager.GetDataCommand("T_Role_GetAllRole");
                list = cmd.ExecuteEntityList<T_ROLE_Entity>();
            }
            catch (Exception ex)
            {
            }
            return list;
        }

        /// <summary>
        /// 根据用户获取角色列表
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public static List<T_ROLE_Entity> T_Role_ListByUserId(System.Int64 UserId)
        {
            List<T_ROLE_Entity> list = new List<T_ROLE_Entity>();
            try
            {
                DataCommand cmd = DataCommandManager.GetDataCommand("T_Role_ListByUserId");
                cmd.SetParameterValue("@USERID", UserId);
                list = cmd.ExecuteEntityList<T_ROLE_Entity>();
            }
            catch (Exception ex)
            {
            }
            return list;
        }
        #endregion

        #region 角色功能关系
        /// <summary>
        /// 根据角色删除功能
        /// </summary>
        /// <param name="RoleId"></param>
        /// <returns></returns>
        public static void T_Role_Function_DeleteByRoleId(System.Decimal RoleId)
        {
            try
            {
                DataCommand cmd = DataCommandManager.GetDataCommand("T_Role_Function_DeleteByRoleId");
                cmd.SetParameterValue("@RoleId", RoleId);
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
            }
        }

        /// <summary>
        /// 新增角色功能关系
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static T_ROLE_FUNCTION_Entity T_Role_Function_Add(T_ROLE_FUNCTION_Entity entity)
        {
            try
            {
                DataCommand cmd = DataCommandManager.GetDataCommand("T_Role_Function_Add");
                cmd.SetParameterValue("@ROLEID", entity.ROLEID);
                cmd.SetParameterValue("@FUNCTIONID", entity.FUNCTIONID);
                cmd.ExecuteNonQuery();
                entity.ID = Convert.ToInt32(cmd.GetParameterValue("@PID"));
            }
            catch (Exception ex)
            {
            }

            return entity;
        }

        /// <summary>
        /// 根据角色获取功能
        /// </summary>
        /// <param name="RoleId"></param>
        public static List<T_ROLE_FUNCTION_Entity> T_Role_Function_ListByRoleId(System.Decimal RoleId)
        {
            List<T_ROLE_FUNCTION_Entity> list = new List<T_ROLE_FUNCTION_Entity>();
            try
            {
                DataCommand cmd = DataCommandManager.GetDataCommand("T_Role_Function_ListByRoleId");
                cmd.SetParameterValue("@ROLEID", RoleId);
                list = cmd.ExecuteEntityList<T_ROLE_FUNCTION_Entity>();
            }
            catch (Exception ex)
            {
            }
            return list;
        }

        /// <summary>
        /// 根据用户获取功能
        /// </summary>
        /// <param name="UserId"></param>
        public static List<T_ROLE_FUNCTION_Entity> T_Role_Function_ListByUserId(System.Decimal UserId)
        {
            List<T_ROLE_FUNCTION_Entity> list = new List<T_ROLE_FUNCTION_Entity>();
            try
            {
                DataCommand cmd = DataCommandManager.GetDataCommand("T_Role_Function_ListByUserId");
                cmd.SetParameterValue("@USERID", UserId);
                list = cmd.ExecuteEntityList<T_ROLE_FUNCTION_Entity>();
            }
            catch (Exception ex)
            {
            }
            return list;
        }
        #endregion

        #region 用户管理
        public static TS_User_Entity T_USER_View(System.Int64 Idx)
        {
            TS_User_Entity c = default(TS_User_Entity);
            try
            {
                DataCommand cmd = DataCommandManager.GetDataCommand("T_USER_View");
                cmd.SetParameterValue("@Idx", Idx);
                c = cmd.ExecuteEntity<TS_User_Entity>();
            }
            catch (Exception ex)
            {
            }
            return c;
        }

        public static TS_User_Entity T_USER_View(System.String UserName)
        {
            TS_User_Entity c = default(TS_User_Entity);
            try
            {
                DataCommand cmd = DataCommandManager.GetDataCommand("T_USER_ViewByUserName");
                cmd.SetParameterValue("@UserName", UserName);
                c = cmd.ExecuteEntity<TS_User_Entity>();
            }
            catch (Exception ex)
            {
            }
            return c;
        }

        public static TS_User_Entity T_USER_Add(TS_User_Entity entity)
        {
            try
            {
                DataCommand cmd = DataCommandManager.GetDataCommand("T_USER_Add");
                cmd.SetParameterValue("@UserName", entity.UserName);
                cmd.SetParameterValue("@Password", entity.Password);
                cmd.SetParameterValue("@Name", entity.Name);
                cmd.SetParameterValue("@Gender", entity.Gender);
                cmd.SetParameterValue("@BirthDay", entity.BirthDay);
                cmd.SetParameterValue("@IdentityCard", entity.IdentityCard);
                cmd.SetParameterValue("@Mobile", entity.Mobile);
                cmd.SetParameterValue("@Email", entity.Email);
                cmd.SetParameterValue("@PhotoImage", entity.PhotoImage);
                cmd.SetParameterValue("@UserType", entity.UserType);
                cmd.ExecuteNonQuery();
                entity.Idx = Convert.ToInt64(cmd.GetParameterValue("@PID"));
            }
            catch (Exception ex)
            {
            }
            return entity;
        }

        public static void T_USER_Update(TS_User_Entity entity)
        {
            try
            {
                DataCommand cmd = DataCommandManager.GetDataCommand("T_USER_Update");
                cmd.SetParameterValue("@Idx", entity.Idx);
                cmd.SetParameterValue("@UserName", entity.UserName);
                cmd.SetParameterValue("@Password", entity.Password);
                cmd.SetParameterValue("@Name", entity.Name);
                cmd.SetParameterValue("@Gender", entity.Gender);
                cmd.SetParameterValue("@BirthDay", entity.BirthDay);
                cmd.SetParameterValue("@IdentityCard", entity.IdentityCard);
                cmd.SetParameterValue("@Mobile", entity.Mobile);
                cmd.SetParameterValue("@Email", entity.Email);
                cmd.SetParameterValue("@PhotoImage", entity.PhotoImage);
                cmd.SetParameterValue("@UserType", entity.UserType);
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
            }
        }

        public static void T_USER_Delete(System.Decimal ID)
        {
            try
            {
                DataCommand cmd = DataCommandManager.GetDataCommand("T_USER_Delete");
                cmd.SetParameterValue("@ID", ID);
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
            }
        }

        /// <summary>
        /// 用户记录数
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static int T_USER_RecordCount(Dictionary<string, object> parameters)
        {
            int RecordCount = 0;
            try
            {
                DataCommand cmd = DataCommandManager.GetDataCommand("T_USER_RecordCount");
                Utility.BuilderCommandParameter(parameters, cmd);
                RecordCount = Convert.ToInt32(cmd.ExecuteScalar());
            }
            catch (Exception ex)
            {
            }
            return RecordCount;
        }

        /// <summary>
        /// 用户分页列表
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public static List<TS_User_Entity> T_User_ListByPaging(Dictionary<string, object> parameters, int pageSize, int pageIndex, out int recordCount)
        {
            List<TS_User_Entity> list = new List<TS_User_Entity>();
            recordCount = 0;
            try
            {
                DataCommand cmd = DataCommandManager.GetDataCommand("T_USER_ListByPaging");
                Utility.BuilderCommandParameter(parameters, cmd);
                cmd.SetParameterValue("@pageSize", pageSize);
                cmd.SetParameterValue("@pageIndex", pageIndex);
                list = cmd.ExecuteEntityList<TS_User_Entity>();
                recordCount = Convert.ToInt32(cmd.GetParameterValue("@recordCount"));
            }
            catch (Exception ex)
            {
            }
            return list;
        }
        #endregion

        #region 用户角色关系
        /// <summary>
        /// 根据用户删除角色
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public static void T_USER_Role_DeleteByUserId(System.Decimal UserId)
        {
            try
            {
                DataCommand cmd = DataCommandManager.GetDataCommand("T_USER_ROLE_DeleteByUserId");
                cmd.SetParameterValue("@USERID", UserId);
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
            }
        }

        /// <summary>
        /// 新增用户角色关系
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static TS_USER_ROLE_Entity T_USER_Role_Add(TS_USER_ROLE_Entity entity)
        {
            try
            {
                DataCommand cmd = DataCommandManager.GetDataCommand("T_USER_ROLE_Add");
                cmd.SetParameterValue("@USERID", entity.USERID);
                cmd.SetParameterValue("@ROLEID", entity.ROLEID);
                cmd.ExecuteNonQuery();
                entity.ID = Convert.ToInt32(cmd.GetParameterValue("@PID"));
            }
            catch (Exception ex)
            {
            }

            return entity;
        }

        /// <summary>
        /// 根据用户获取角色
        /// </summary>
        /// <param name="RoleId"></param>
        public static List<TS_USER_ROLE_Entity> T_USER_Role_ListByUserId(System.Decimal UserId)
        {
            List<TS_USER_ROLE_Entity> list = new List<TS_USER_ROLE_Entity>();
            try
            {
                DataCommand cmd = DataCommandManager.GetDataCommand("T_USER_ROLE_ListByUserId");
                cmd.SetParameterValue("@USERID", UserId);
                list = cmd.ExecuteEntityList<TS_USER_ROLE_Entity>();
            }
            catch (Exception ex)
            {
            }
            return list;
        }
        #endregion
    }
}
